package cn.elead.chaos.sso.sample.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.elead.chaos.framework.properties.ChaosProperties;
import cn.elead.chaos.framework.properties.Sso;
import cn.elead.chaos.sso.core.constant.SsoConstant;
import cn.elead.chaos.sso.core.filter.SsoWebFilter;
import cn.elead.chaos.sso.core.handler.LoginSuccessHandler;
import cn.elead.chaos.sso.sample.handler.LoginSuccessHandlerImpl;

/**
 * 
 * @author luopeng
 *
 */
@Configuration
public class SsoConfig {

	@Autowired
	private ChaosProperties chaos;
	

	/**
	 * 单点登录成功回调
	 * 
	 * @return
	 */
	@Bean
	public LoginSuccessHandler loginSuccessHandler() {
		return new LoginSuccessHandlerImpl();
	}

	@Bean
	public FilterRegistrationBean<SsoWebFilter> SsoFilterRegistration() {

		FilterRegistrationBean<SsoWebFilter> registration = new FilterRegistrationBean<SsoWebFilter>();

		registration.setName("SsoOauthFilter");
		registration.setOrder(1);
		registration.addUrlPatterns("/*");

		SsoWebFilter ssoWebFilter = new SsoWebFilter();
		ssoWebFilter.setLoginSuccessHandler(loginSuccessHandler());
		registration.setFilter(ssoWebFilter);

		Sso sso = chaos.getSso();
		registration.addInitParameter(SsoConstant.SSO_SERVER, sso.getServer());
		registration.addInitParameter(SsoConstant.PARAM_CLIENT_ID, sso.getClientId()); // 单点退出路径
		registration.addInitParameter(SsoConstant.SSO_CLIENT_SECRET, sso.getClientSecret()); // 通过token换取token说
		registration.addInitParameter(SsoConstant.SSO_LOGOUT, sso.getLogout()); // 前端配置获取用户信息的路径
		registration.addInitParameter(SsoConstant.PARAM_RESPONSE_TYPE, sso.getResponseType());

		return registration;
	}

}
